/*
 * @Description: 抓取vova平台数据
 * @Author: zhangzhengzhe
 * @LastEditors: Please set LastEditors
 * @Date: 2019-03-20 19:51:23
 * @LastEditTime: 2019-03-21 19:09:04
 */

// 获取vova登录数据
const puppeteer = require("puppeteer");
(async  () => {
    const width = 1920
    const height = 938
    let timeout = function (delay) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                try {
                    resolve(1)
                } catch (e) {
                    reject(0)
                }
            }, delay);
        })
    }
    const browser = await puppeteer.launch({
        headless: false, // 开启界面
        devtools: true,  // 开启开发者控制台
        // slowMo: 250
    });
    const page = await browser.newPage();
    // await page.setViewport({
    //     width: width,
    //     height: height
    // })
    page.setDefaultNavigationTimeout( 90000 );
    // waitUtil:load页面加载完成是触发
    await page.goto("https://merchant.vova.com.hk/index.php?q=admin/main/index/login", { waitUntil: 'load'});

    // 登录
    await page.type("#inputEmail", "vbshuqw@hotmail.com");
    await page.type("#inputPassword", "pzwg@123456STK");

    await page.click("#btn_submit");
    await timeout(1000);

    // 监听console输出
    // page.on('console', msg => console.log('PAGE LOG:', ...msg.args));
    // await page.evaluate(() => console.log(`url is ${location.href}`));
    // 关闭首页弹窗
    // await page.click("#pop-notice-modal")
    // await timeout(1000)
    // 首页列表获取及点击
    // await page.addScriptTag({
    //         url: 'https://cdn.bootcss.com/jquery/3.2.0/jquery.min.js',
    //         type: "module"
    //     })
    // 进入商品列表页
    await page.goto("https://merchant.vova.com.hk/index.php?q=admin/main/product/index&perPage=100", {waitUntil: "load"});
    await timeout(1000);
    // 关闭商品列表中的弹窗
    await page.click("#modifySaleCategory");
    await timeout(1000);
    //商品列表信息获取
    const body = document.querySelectorAll("#productListPage table#prodlist-table tbody tr");
        console.log(body.length);
    // await page.waitForNavigation({waitUntil: "load"});
    //页面登录成功后，是否需要reload 根据实际情况来确定
    // await page.reload();
})()